package leetcode.hot100;

import java.util.PriorityQueue;

public class Solution215 {

    public static void main(String[] args) {

    }

    public int findKthLargest(int[] nums, int k) {
        //维护一个小根堆，堆里有n-k个元素时，把要入堆的元素和堆顶比较；
        //若要入堆大于堆顶，则弹出堆顶，将要入堆的元素入堆
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        for (int num: nums) {
            if(minHeap.size()==k&&num>minHeap.peek()) minHeap.poll();
            if(minHeap.size()<k) minHeap.offer(num);
        }
        return minHeap.peek();
    }
}
